require("../../assets/css/index.less")
require("../../assets/lib/swiper/swiper.css")
require('weui')
require("./my.less")

const weui = require("weui.js")
const Swiper = require("../../assets/lib/swiper/swiper.js")
const { getEle, publicFooter, isLogin, ready, addEvent } = require("../../utils/dom")
const { get, post, BASE_URL } = require('../../utils/https')
const { clear } = require('../..//utils/local')
isLogin()

ready(() => {
    publicFooter('my')

    new Swiper('.swiper', {
        loop: true,
        pagination: {
            el: '.swiper-pagination',
        },
        autoplay: {
            delay: 3000,
            stopOnLastSlide: false,
            disableOnInteraction: false,
        }
    })


    function getInfo() {
        get('/api/user/info', res => {
            let { errno, data } = res
            if (errno == 0) {
                getEle("#imgUrl").src = BASE_URL + data.imgUrl

                getEle("#nickName").innerHTML = data.nickName
                getEle("#sign").innerHTML = data.sign
                getEle("#clockCount").innerHTML = data['member.clockCount']
                getEle("#circlesCount").innerHTML = `${data.circlesCount}条`
                getEle("#duration").innerHTML = data.duration
                getEle("#calorie").innerHTML = data.calorie

            }
        })

    }
    getInfo()

    function getClockInfo() {
        get("/api/user/clockInInfo", res => {
            let { errno, data } = res
            console.log(data)
            if (errno ==0) {
                getEle("#clockCount").innerHTML = data.clockCount
                getEle("#clockInBtn").innerHTML = data.isClockIn ? '已打卡' : '今日打卡'
                console.log(data.isClockIn)
            }
        })
    }
    getClockInfo()

    addEvent("#clockInBtn", () => {
        post("/api/user/clockIn", {}, res => {
            let { errno, data } = res
            if (errno == 0) {
                weui.toast('恭喜您,打卡成功!!')
                getClockInfo()
            } else {
                weui.alert("今日已打卡")
            }
        })
    })
    addEvent("#exitBtn", () => {
        clear()
        location.href = 'login.html'
    })
    addEvent("#changeInfo",()=>location.href='changeInfo.html')
    addEvent("#sportData", () => location.href ='sportData.html')


})